package jco3.core;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;

public class SapExecutor {
	
	private SapProxy proxy;
	
	public SapExecutor(SapProxy proxy) {
		this.proxy = proxy;
	}
	
	public Object processSap(JCoDestination destination,JCoFunction function) throws Exception{
		if(!doBefore()) {
			throw new Exception("被拦截器终止！");
		}
		try {
			paresPostData(function);
			JCoFunction func = execFunc(function,destination);
			Object rtn = parseRtnData(func);
			return rtn;
		} catch (Exception e) {
			doErroe(e);
		}finally {
			doAfter();
		}
		return null;
	}
	
	public JCoFunction execFunc(JCoFunction func,JCoDestination destination) throws Exception{
		func.execute(destination);
		return func;
	}
	
	public Object paresPostData(Object soapTemplate) {
		return this.proxy.parsePostData(soapTemplate);
	}
	
	public Object parseRtnData(Object data) {
		return this.proxy.parseRtnData(data);
	}
	
	public boolean doBefore() {
		return this.proxy.daBefore();
	}
	
	public void doAfter() {
		this.proxy.doAfter();
	}
	
	public void doErroe(Exception e) {
		this.proxy.doError(e);
	}
	
}